package com.chinalife.project.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.TimeUnit;

@SpringBootConfiguration
@EnableCaching  //开启注解扫描
public class CaffeineCacheConfig {

    /**
     * @author synhard
     * @description 默认缓存
     * @date 2024/7/29 15:10
     * @param
     * @return org.springframework.cache.caffeine.CaffeineCacheManager
     */
    @Bean("defaultCacheManager")
    public CaffeineCacheManager defaultCacheManager() {
        CaffeineCacheManager defaultCacheManager = new CaffeineCacheManager("project-cache"); //指定这个缓存的命名，这里我是使用了一个常量类
        defaultCacheManager.setAllowNullValues(Boolean.TRUE); // 允许空值的缓存
        Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
                .initialCapacity(10000) // 初始容量缓存条数
                .maximumSize(100000L) // 最大容量缓存条数
                .expireAfterWrite(3, TimeUnit.MINUTES); // 过期时间
        defaultCacheManager.setCaffeine(caffeineBuilder);
        return defaultCacheManager;
    }
}